package com.xiong.wuliao;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.squareup.picasso.Picasso;
import com.xiong.wuliao.entity.Goods;
import com.xiong.wuliao.entity.Urls;
import com.xiong.wuliao.util.GoodsJSON;
import com.xiong.wuliao.view.MyGridView;
import com.xiong.wuliao.view.MyPullToView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 单品购买页面，由分类页面单品购买点击跳转
 */
public class PurchaseActivity extends AppCompatActivity {
	private MyPullToView myPullToView; // 自定义组合控件
	private MyGridView myGridView;  // 自定义GridView
	private MyAdapter myAdapter; // 给GridView的自定义适配器

	private ImageButton myPurchaseButtonGoodBack;
	private int offset = 0; // 上拉刷新的变化参数
	private List<Goods> goodsList = new ArrayList<>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_purchase);
		init();
	}

	private void init() {
		findView();
		setGridView(); // 设置GridView属性
		initData(); // 初始化数据
		setMyAdapter(); // 设置适配器
		setListener();
	}

	private void setListener() {
		myGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Goods goods = goodsList.get(position);
				String s = goods.getId();
				Intent intent = new Intent(PurchaseActivity.this, GoodActivity.class);
				intent.putExtra("good", s);
				startActivity(intent);
			}
		});

		myPurchaseButtonGoodBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				PurchaseActivity.this.finish();
			}
		});
	}

	private void setMyAdapter() {
		myAdapter = new MyAdapter(PurchaseActivity.this, goodsList);
		myGridView.setAdapter(myAdapter);
	}

	private void initData() {
		getGoodsSaveData();
	}

	private void setGridView() {
		myPullToView.setGridViewNum(2); // 设置为2列
		myPullToView.setHorizontalSpacing(20); // 设置水平距离
		myPullToView.setVerticalSpacing(20); // 设置垂直距离
	}

	private void findView() {
		myPullToView = (MyPullToView) findViewById(R.id.myPullView);
		myGridView = (MyGridView) findViewById(R.id.myGridView);
		myPurchaseButtonGoodBack = (ImageButton) findViewById(R.id.myPurchaseButtonGoodBack);
	}

	/**
	 * 获取单品页面的服务器数据
	 */
	private void getGoodsSaveData() {
		OkHttpUtils.get(Urls.GoodsPath)
				.params("gender", 2)
				.params("limit", 20)
				.params("offset", offset)
				.params("generation", 2)
				.execute(new StringCallback() {
					@Override
					public void onSuccess(String s, Call call, Response response) {
						JSONObject jsonObject = null;
						try {
							jsonObject = new JSONObject(s.trim());
							String result = jsonObject.getString("message");
							if (result.equals("OK")) {
								goodsList.addAll(GoodsJSON.saveGoodsData(s.trim()));

								myAdapter.notifyDataSetChanged();
							} else {
								Toast.makeText(PurchaseActivity.this, "数据请求失败", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	/**
	 * 给GridView的自定义适配器
	 */
	private class MyAdapter extends BaseAdapter {
		LayoutInflater inflater;
		Context context;
		List<Goods> goodsList;

		public MyAdapter(Context context, List<Goods> goodsList) {
			this.context = context;
			this.goodsList = goodsList;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return goodsList.size();
		}

		@Override
		public Object getItem(int position) {
			return goodsList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Goods goods = goodsList.get(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.goods_adapter_item, null);
				holder = new ViewHolder();
				holder.itemGoodsImageView = (ImageView) convertView.findViewById(R.id.itemGoodsImageView);
				holder.itemGoodsTitleText = (TextView) convertView.findViewById(R.id.itemGoodsTitleText);
				holder.itemGoodsPrice = (TextView) convertView.findViewById(R.id.itemGoodsPrice);
				holder.itemGoodsAgreeText = (TextView) convertView.findViewById(R.id.itemGoodsAgreeText);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			Picasso.with(context).load(goods.getCover_image_url()).into(holder.itemGoodsImageView);
			holder.itemGoodsTitleText.setText(goods.getName());
			holder.itemGoodsPrice.setText(goods.getPrice());
			holder.itemGoodsAgreeText.setText(goods.getFavorites_count());

			return convertView;
		}
	}

	static class ViewHolder {
		public ImageView itemGoodsImageView; // 商品封面图片
		public TextView itemGoodsTitleText; //商品封面标题
		public TextView itemGoodsPrice;  //商品价格
		public TextView itemGoodsAgreeText;  //商品点赞次数
	}
}
